<?php
require_once("mysqlUser/mysql.php");
require_once("secWeaponBase.php");

class secWeapon
{
	public static $table = "secweapondepot";
	//////////////////////////////////////////////////
	/**
	* 给玩家一个新的副武器
	* @param mixed $uid 玩家id
	* @param mixed $id 副武器id
	* @param mixed $inc 强化等级
	*/	
	public static function giveToUser($uid,$id,$inc)
	{
		$base = new secWeaponBase($id,$inc);
		$dur = $base->maxdur;
		
		$command =  "INSERT INTO ".secWeapon::$table." VALUES (null,$uid,$id,$inc,$dur)"; 
		mysql::run($command);
	}
	
	//////////////////////////////////////////////////
	private $pid;
	private $uid;
	private $id;
	private $inc;
	private $dur;
	
	private $baseValues;
	//////////////////////////////////////////////////
    public function __set($name, $value) 
    {
		if (isset($this->$name))
        {       	
			if($name=='uid'|| $name=='inc'|| $name=='dur') 
			{
				$command = "update ".secWeapon::$table." set $name=$value where pid=$this->pid ";
				mysql::run($command);
			}
        } 
		
         $this->$name = $value;
    }
    public function __get($name) 
    {
         if( isset( $this->$name ) ) 
         {    
			if($name=='uid'|| $name=='inc'|| $name=='dur') 
			{
				$command = "select * from ".secWeapon::$table." where pid=$this->pid";
				$result =  mysql::run($command);
				$vals=mysql_fetch_array($result);
				$this->$name = $vals[$name];
			}
			
            return $this->$name;
			
         } else {
            trigger_error( $name . ' variables undefined',  E_USER_NOTICE );
         }
    }
	//////////////////////////////////////////////////
	/**
	 * 玩家拥有的副武器
	 */
	public function secWeapon($pid)
	{		
		$this->pid=$pid;
		
		$command = "select * from ".secWeapon::$table." where pid=$pid";
  		$result = mysql::run($command);
    	$vals=mysql_fetch_array($result);
		
		$data=array("uid"=>$vals["uid"],"id"=>$vals["id"],"inc"=>$vals["inc"],"dur"=>$vals["dur"]);  	
		
		$this->uid=$data["uid"];
		$this->id=$data["id"];
		$this->inc=$data["inc"];
    	$this->dur = $data["dur"];
		
		$this->baseValues = new secWeaponBase($this->id,$this->inc);
	}
	
	public function drop()
	{
		$command = "delete from ".secWeapon::$table." where pid=$this->pid";
    	mysql::run($command);
	}
}

?>